#include "order_view_scr.h"
#include "main_screen.h"


// 全局变量
static lv_obj_t *scr_order_view = NULL;
static lv_obj_t *order_list = NULL;
static lv_obj_t *modal_bg = NULL;
static lv_obj_t *qr_modal = NULL;
static lv_obj_t *qr_code = NULL;

// 关闭订单查看界面
void    close_order_view_screen(void)
{
    
    if (modal_bg) 
    {
        lv_obj_del(modal_bg);
        modal_bg = NULL;
    }
    
    if (qr_modal) {
        lv_obj_del(qr_modal);
        qr_modal = NULL;
        qr_code = NULL;
    }
}

// 关闭二维码模态窗口
static void close_qr_modal_cb(lv_event_t *e)
{
    if (qr_modal) {
        lv_obj_del(qr_modal);
        qr_modal = NULL;
        qr_code = NULL;
    }
    
    if (modal_bg) {
        lv_obj_del(modal_bg);
        modal_bg = NULL;
    }
}

// 创建模态背景
static void create_modal_bg(lv_obj_t *parent)
{
    if (modal_bg) 
    {
        lv_obj_del(modal_bg);
        modal_bg = NULL;
    }
    
    modal_bg = lv_obj_create(parent);
    lv_obj_set_size(modal_bg, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_bg_opa(modal_bg, LV_OPA_50, 0);
    lv_obj_set_style_bg_color(modal_bg, lv_color_black(), 0);
    lv_obj_set_style_border_width(modal_bg, 0, 0);
    lv_obj_set_style_radius(modal_bg, 0, 0);
    lv_obj_clear_flag(modal_bg, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(modal_bg, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_move_foreground(modal_bg);
}

// 显示二维码窗口
static void show_qr_code(UserOrder *order)
{
    if (!order) return;
    
    // 创建模态背景
    create_modal_bg(lv_layer_top());
    
    // 创建二维码模态窗口
    qr_modal = lv_obj_create(lv_layer_top());
    lv_obj_set_size(qr_modal, 500, 600);
    lv_obj_center(qr_modal);
    lv_obj_set_style_bg_opa(qr_modal, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(qr_modal, lv_color_hex(0x2D2D3A), 0);
    lv_obj_set_style_border_width(qr_modal, 2, 0);
    lv_obj_set_style_border_color(qr_modal, lv_color_white(), 0);
    lv_obj_set_style_radius(qr_modal, 10, 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(qr_modal);
    lv_label_set_text(title, "订单详情");
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
    
    // 订单信息
    char info_text[512];
    snprintf(info_text, sizeof(info_text),
             "车次: %s\n"
             "日期: %s\n"
             "路线: %s - %s\n"
             "时间: %s - %s (%s)\n"
             "座位号: %d",
             order->TrainNumber,
             order->date,
             order->start, order->end,
             order->DepartTime, order->ArriveTime, order->TimeDifference,
             order->seatNumber);
    
    lv_obj_t *info_label = lv_label_create(qr_modal);
    lv_label_set_text(info_label, info_text);
    lv_obj_set_style_text_font(info_label, font_chinese, 0);
    lv_obj_set_style_text_color(info_label, lv_color_white(), 0);
    lv_obj_align(info_label, LV_ALIGN_TOP_LEFT, 20, 60);
    lv_obj_set_width(info_label, 460);
    lv_label_set_long_mode(info_label, LV_LABEL_LONG_WRAP);
    
    // 生成二维码内容
    char qr_data[256];
    snprintf(qr_data, sizeof(qr_data),
             "车次:%s|日期:%s|%s-%s|座位:%d|时间:%s-%s",
             order->TrainNumber,
             order->date,
             order->start, order->end,
             order->seatNumber,
             order->DepartTime, order->ArriveTime);
    
    // 创建二维码
    lv_color_t bg_color = lv_palette_lighten(LV_PALETTE_LIGHT_BLUE, 5);
    lv_color_t fg_color = lv_palette_darken(LV_PALETTE_BLUE, 4);
    
    qr_code = lv_qrcode_create(qr_modal, 200, fg_color, bg_color);
    lv_qrcode_update(qr_code, qr_data, strlen(qr_data));
    lv_obj_align(qr_code, LV_ALIGN_BOTTOM_MID, 0, -70);
    
    // 添加边框
    lv_obj_set_style_border_color(qr_code, bg_color, 0);
    lv_obj_set_style_border_width(qr_code, 5, 0);
    
    // 关闭按钮
    lv_obj_t *close_btn = lv_btn_create(qr_modal);
    lv_obj_set_size(close_btn, 100, 40);
    lv_obj_align(close_btn, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_style_bg_color(close_btn, lv_color_hex(0xF44336), 0);
    lv_obj_set_style_radius(close_btn, 10, 0);
    
    lv_obj_t *close_label = lv_label_create(close_btn);
    lv_label_set_text(close_label, "关闭");
    lv_obj_set_style_text_font(close_label, font_chinese, 0);
    lv_obj_center(close_label);
    lv_obj_add_event_cb(close_btn, close_qr_modal_cb, LV_EVENT_CLICKED, NULL);
}

// 订单项点击事件
static void order_item_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED) {
        lv_obj_t *btn = lv_event_get_target(e);
        UserOrder *order = (UserOrder *)lv_obj_get_user_data(btn);
        
        if (order != NULL) {
            show_qr_code(order);
        }
    }
}

// 返回按钮事件
// static void return_btn_event_cb(lv_event_t *e)
// {
//     lv_event_code_t code = lv_event_get_code(e);
//     if (code == LV_EVENT_CLICKED) 
//     {
//         close_order_view_screen();
//         lv_scr_load(scr_main);
//     }
// }

static void return_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) 
    {
        close_order_view_screen();

        //  加保护：判断 scr_main 是否有效
        if (scr_main && lv_obj_is_valid(scr_main)) 
        {
            lv_scr_load(scr_main);
        } else
         {
            printf("[ERROR] scr_main is NULL or invalid, fallback to login\n");
            lv_scr_load(scr_login); // 或者重新创建主界面
        }
    }
}

// 添加订单到列表
static void add_order_to_list(UserOrder *order)
{
    if (!order || !order_list) return;
    
    char buffer[512];
    snprintf(buffer, sizeof(buffer), 
             "%s  日期:%s %s-%s %s-%s 座位:%d",
             order->TrainNumber,
             order->date,
             order->start, order->end,
             order->DepartTime, order->ArriveTime,
             order->seatNumber);
    
    lv_obj_t *btn = lv_list_add_btn(order_list, NULL, buffer);
    lv_obj_set_user_data(btn, order);
    lv_obj_set_style_text_font(btn, font_chinese, 0);
    lv_obj_add_event_cb(btn, order_item_event_cb, LV_EVENT_CLICKED, NULL);
}

// 订单查看界面
void show_order_view_screen(void)
{
    // 关闭可能已存在的界面
    close_order_view_screen();
    
    // 创建订单查看界面
    scr_order_view = lv_obj_create(NULL);
    lv_obj_add_style(scr_order_view, &bg_style, 0);
    lv_obj_clear_flag(scr_order_view, LV_OBJ_FLAG_SCROLLABLE);
    lv_scr_load(scr_order_view);
    
    // 标题
    lv_obj_t *title = lv_label_create(scr_order_view);
    lv_label_set_text(title, "我的订单");
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
    
    // 订单列表
    order_list = lv_list_create(scr_order_view);
    lv_obj_set_size(order_list, 1000, 500);
    lv_obj_align(order_list, LV_ALIGN_TOP_MID, 0, 70);
    lv_obj_set_style_border_width(order_list, 1, 0);
    lv_obj_set_style_border_color(order_list, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_radius(order_list, 10, 0);
    
    // 获取当前用户的订单
    User *current_user = findUser(userList, user);
    if (current_user && current_user->orders) {
        for (int i = 0; i < current_user->orderCount; i++)
         {
            add_order_to_list(&current_user->orders[i]);
        }
    } else {
        // 没有订单的提示
        lv_obj_t *btn = lv_list_add_btn(order_list, NULL, "暂无订单");
        lv_obj_set_style_text_font(btn, font_chinese, 0);
        lv_obj_add_state(btn, LV_STATE_DISABLED);
    }
    
    // 返回按钮
    lv_obj_t *return_btn = lv_btn_create(scr_order_view);
    lv_obj_set_size(return_btn, 150, 60);
    lv_obj_align(return_btn, LV_ALIGN_TOP_RIGHT, -20, 10);
    lv_obj_set_style_bg_color(return_btn, lv_color_hex(0xF44336), 0);
    lv_obj_set_style_radius(return_btn, 10, 0);
    
    lv_obj_t *return_label = lv_label_create(return_btn);
    lv_label_set_text(return_label, "返回主界面");
    lv_obj_set_style_text_font(return_label, font_chinese, 0);
    lv_obj_set_style_text_color(return_label, lv_color_white(), 0);
    lv_obj_center(return_label);
    
    lv_obj_add_event_cb(return_btn, return_btn_event_cb, LV_EVENT_CLICKED, NULL);
}